# Copyright (c) 2018 Presto Labs Pte. Ltd.
# Author: xguo

import coin.exchange.base.kr_rest.native_public_client_base as npubcb
import coin.exchange.liqui_v3.kr_rest.constants as exchange_constants
from coin.base.param_util import to_list


class LiquiNativePublicClient(npubcb.NativePublicClientBase):
  def __init__(self, timeout=3):
    npubcb.NativePublicClientBase.__init__(self, url='https://api.liqui.io/api/3', timeout=timeout)

  @staticmethod
  def _to_url(method_name, symbol_list=None):
    symbol_list = symbol_list or []
    symbol_list = to_list(symbol_list)
    return '%s/%s' % (method_name, '-'.join(symbol_list))

  def get_info(self):
    url = self._to_url('info')
    update = self._get_as_json(url)
    return update

  def get_ticker(self, symbol_list):
    url = self._to_url('ticker', symbol_list)
    update = self._get_as_json(url)
    return update

  def get_orderbook(self, symbol_list, limit=150):
    assert 1 <= limit <= 2000, 'Invalid size.'
    url = self._to_url('depth', symbol_list)
    params = {'limit': limit}
    update = self._get_as_json(url, params)
    return update

  def get_trades(self, symbol_list, limit=150):
    assert 1 <= limit <= 2000, 'Invalid size.'
    url = self._to_url('trade', symbol_list)
    params = {'limit': limit}
    update = self._get_as_json(url, params)
    return update

  def queryAll(self):
    for symbol in exchange_constants.symbol_list[:10]:
      print(self.get_orderbook(symbol))


if __name__ == "__main__":
  client = LiquiNativePublicClient()
  print(client.get_ticker('ltc_btc'))
  client.queryAll()
